package org.nlp.algo.classifier;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import com.aliasi.classify.ConfusionMatrix;
import com.aliasi.classify.ScoredClassification;
import com.aliasi.classify.ScoredClassifier;
import com.aliasi.util.Files;

public class TestClassifier {
	// 测试语料的存放目录
	private static String corpusPath = "resource/corpus/test";
	private static File TDIR = new File(corpusPath);
	private static String modelFile = "NaiveBayesClassifier"; //用贝叶斯模型
	
	// 定义分类
	private static String[] CATEGORIES;// = { "汽车", "财经", "IT", "健康", "体育", "旅游","教育", "招聘", "文化", "军事" };

	@SuppressWarnings({ "unchecked" })
	public static void main(String[] args) throws ClassNotFoundException {

		// 分类器模型存放地址
		
		ScoredClassifier<CharSequence> compiledClassifier = null;
		try {
			ObjectInputStream oi = new ObjectInputStream(new FileInputStream(
					modelFile));
			compiledClassifier = (ScoredClassifier<CharSequence>) oi
					.readObject();
			oi.close();
		} catch (IOException ie) {
			System.out
					.println("IO Error: Model file " + modelFile + " missing");
		}

		List<String> catgroies = new ArrayList<String>();
		int catgoriesCount = 0;
		
		File[] tranTypes = TDIR.listFiles();		
		for (int i = 0; i < tranTypes.length; i++) {
			if (tranTypes[i].isDirectory()
					&& !tranTypes[i].getName().startsWith(".")) {	
				catgroies.add(tranTypes[i].toString().replace(tranTypes[i].getParent()+"\\", ""));
				System.out.println(tranTypes[catgoriesCount]);
				catgoriesCount++;
			}
		}
		CATEGORIES = new String[catgroies.size()];
		catgroies.toArray(CATEGORIES);
		
		// 遍历分类目录中的文件测试分类准确度
		ConfusionMatrix confMatrix = new ConfusionMatrix(CATEGORIES);
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumIntegerDigits(1);
		nf.setMaximumFractionDigits(3);
		for (int i = 0; i < CATEGORIES.length; ++i) {
			File classDir = new File(TDIR, CATEGORIES[i]);

			// 对于每一个文件，通过分类器找出最适合的分类
			for (File file : classDir.listFiles()) {
				String text = "";
				try {
					text = Files.readFromFile(file, "gbk");
				
				System.out.println("测试 " + CATEGORIES[i] + File.separator
						+ file.getName());

				ScoredClassification classification = compiledClassifier
						.classify(text.subSequence(0, text.length()));
				confMatrix.increment(CATEGORIES[i],
						classification.bestCategory());
				System.out.println("最适合的分类: " + classification.bestCategory());
				} catch (Exception e) {
					System.out.println("不能读取 " + file.getName());
					continue;
				}
			}
		}

		System.out.println("--------------------------------------------");
		System.out.println("- TestClassifier 结果 ");
		System.out.println("--------------------------------------------");
		int[][] imatrix = confMatrix.matrix();
		StringBuffer sb = new StringBuffer();
		sb.append(StringTools.fillin("CATEGORY", 10, true, ' '));
		for (int i = 0; i < CATEGORIES.length; i++)
			sb.append(StringTools.fillin(CATEGORIES[i], 8, false, ' '));
		System.out.println(sb.toString());

		for (int i = 0; i < imatrix.length; i++) {
			sb = new StringBuffer();
			sb.append(StringTools.fillin(CATEGORIES[i], 10, true, ' ',
					10 - CATEGORIES[i].length()));
			for (int j = 0; j < imatrix.length; j++) {
				String out = "" + imatrix[i][j];
				sb.append(StringTools.fillin(out, 8, false, ' ',
						8 - out.length()));
			}
			System.out.println(sb.toString());
		}

		System.out.println("准确度: " + nf.format(confMatrix.totalAccuracy()));
		System.out.println("总共正确数 : " + confMatrix.totalCorrect());
		System.out.println("总数：" + confMatrix.totalCount());
	}
}
